#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <functional>

using namespace std;
using namespace std::placeholders;

ostream& print(ostream &, const string &, char);

int main()
{
    vector<string> words{"a", "bb", "ccc"};
    ostream &os(cout);
    char c = ' ';
    // os 是一个局部变量，引用一个输出流
    // c 是一个局部变量，类型为 char
    for_each(words.begin(), words.end(), 
            [&os, c] (const string &s) { os << s << c; });
    cout << endl;

    // 错误：bind 会拷贝其参数，但 os 不能拷贝
    // for_each(words.begin(), words.end(), bind(print, os, _1, c));
    // ref 返回的对象包含原对象的引用，可以拷贝，cref 返回的对象包含 const 引用
    for_each(words.begin(), words.end(), bind(print, ref(os), _1, c));
}

ostream& print(ostream &os, const string &s, char c)
{
    return os << s << c;
}